package com.own.demo.坐标转换;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.enumv2.GeoPoint;
import com.own.demo.enumv2.GpsTypeEnums;
import com.own.demo.writerfile.GeoJsonToOsmXML;
import com.own.demo.utils.CommWriterFileInfo;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.vo.Bounds;

import java.io.BufferedWriter;
import java.io.File;

public class 长沙路网转02坐标 {
    private static String geojsonUrl="E:\\ownCode\\UMA\\数据";
    private static String geoJsonName="\\长沙市-gcj02.geojson";

    private static String osmUrl="E:\\ownCode\\UMA\\uam\\uam-road-engine";
    private static String osmName="\\长沙市.geojson.osm";

    public static void main(String[] args) {

        File geoZJ = new File("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\坐标转换\\长沙市-wgs84.geojson");
        JSONObject  geoData = JSONObject.parseObject(FileToStringUtils.txt2String(geoZJ));
        System.out.println(geoData.size());
        JSONArray jsonArray= geoData.getJSONArray("features");
        System.out.println("jssize:"+jsonArray.size());
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject=jsonArray.getJSONObject(i);

            JSONObject properties=jsonObject.getJSONObject("properties");
            String kind=properties.getString("KIND");
            properties.remove("KIND");
            properties.put("kind",kind);

            String name=properties.getString("NAME");
            properties.remove("NAME");
            properties.put("name",name);


            JSONObject geometry= jsonObject.getJSONObject("geometry");
            JSONArray coordinates=geometry.getJSONArray("coordinates");
            int row=coordinates.size();

            JSONArray coordinatesO2=new JSONArray(row);
            for(int h=0;h<row;h++){
                JSONArray temp= coordinates.getJSONArray(h);
                double lon = temp.getDouble(0);
                double lat = temp.getDouble(1);
                GeoPoint gpsPoint = GpsTypeEnums.gpsToGcJo2New(lat, lon, GpsTypeEnums.COORDINATE_SYSTEM_WGS84.getId());

                JSONArray temp02=new JSONArray(4);
                temp02.add(gpsPoint.getX());
                temp02.add(gpsPoint.getY());
                coordinatesO2.add(temp02);
            }
            geometry.remove("coordinates");
            geometry.put("coordinates",coordinatesO2);
        }

        JSONObject jsonObject=长沙市区域.getArea();
//        geoData.getJSONArray("features").add(jsonObject.getJSONArray("features"));
        String json=JSONObject.toJSONString(geoData);
        writerFoFile(json,geojsonUrl,geoJsonName);
//        geoData.getJSONArray("features").removeAll(jsonObject.getJSONArray("features"));

        //地图加载
//        writerFoFile("var polylineData1 = " + json,"E:\\ownCode\\UMA\\uam\\地图工具-路网","\\polylineData1.js");

        GeoJsonToOsmXML.jsonToXML(geoData,(Bounds)jsonObject.get("bounds"),osmUrl,osmName);

    }

    public static void writerFoFile(String data, String folderPath, String fileName) {
        try {
            File folder = new File(folderPath);

            if (!folder.exists()) {
                folder.mkdirs();
            }
            BufferedWriter bufferedWriter = CommWriterFileInfo.getWriterBuffer(folderPath + "\\" + fileName, false);
            CommWriterFileInfo.writer(bufferedWriter, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
